#include <iostream>
#include <fstream>
#include <vector>
#include <filesystem>
#include "tire.h"
#include "chassis.h"
#include "agx_kit.h"
#include "camera.h"
#include "lidar.h"
#include "gyroscope.h"
#include "lcd.h"
#include "battery.h"
#include "student.h"

class Car {
public:
    Chassis chassis;
    AGXKit agxKit;
    Camera camera;
    Lidar lidar;
    Gyroscope gyroscope;
    LCD lcd;
    Battery battery;
    Student assignedStudent;

    void print() const {
        std::cout << "===== 车辆配置信息 =====" << std::endl;
        chassis.print();
        agxKit.print();
        camera.print();
        lidar.print();
        gyroscope.print();
        lcd.print();
        battery.print();
        std::cout << "分配学生: ";
        assignedStudent.print();
    }

    void save(std::ofstream& out) const {
        out << "===== 车辆配置信息 =====" << std::endl;
        chassis.save(out);
        agxKit.save(out);
        camera.save(out);
        lidar.save(out);
        gyroscope.save(out);
        lcd.save(out);
        battery.save(out);
        out << "分配学生: ";
        assignedStudent.save(out);
    }
};

void initCar(Car& car, int index, bool debug = false) {
    // 设置轮胎属性
    Tire tire;
    tire.setType(debug ? "测试轮胎" + std::to_string(index) : "高性能轮胎" + std::to_string(index));
    tire.setSize("235/55 R18");

    // 设置底盘属性
    car.chassis.setId(debug ? "TEST" + std::to_string(index) : "CH00" + std::to_string(index));
    car.chassis.setModel("标准底盘");
    car.chassis.setWheelbase("2800mm");
    car.chassis.setTrack("1600mm");
    car.chassis.setMinClearance("180mm");
    car.chassis.setMinTurnRadius("5.5m");
    car.chassis.setDriveType("四驱");
    car.chassis.setMaxRange("500km");
    car.chassis.addTire(tire);

    // 设置其他模块属性...
    // (保留原有设置逻辑，添加debug模式支持)
}

void assignCars(std::vector<Car>& cars) {
    int current = 0;
    while (true) {
        cars[current].print();
        std::cout << "\n操作选项:" << std::endl;
        std::cout << "1. 修改学生信息" << std::endl;
        std::cout << "n. 下一辆车" << std::endl;
        std::cout << "p. 上一辆车" << std::endl;
        std::cout << "q. 退出分配" << std::endl;
        std::cout << "请选择操作: ";

        char cmd;
        std::cin >> cmd;

        switch (cmd) {
            case '1': {
                Student student;
                std::cout << "请输入学号: ";
                std::cin >> student.id;
                std::cout << "请输入姓名: ";
                std::cin >> student.name;
                cars[current].assignedStudent = student;
                break;
            }
            case 'n':
                if (current < cars.size() - 1) current++;
                else std::cout << "已经是最后一辆车" << std::endl;
                break;
            case 'p':
                if (current > 0) current--;
                else std::cout << "已经是第一辆车" << std::endl;
                break;
            case 'q':
                return;
            default:
                std::cout << "无效输入" << std::endl;
        }
    }
}

int main(int argc, char* argv[]) {
    bool debug = false;
    for (int i = 1; i < argc; ++i) {
        std::string arg = argv[i];
        if (arg == "--debug" || arg == "-d") {
            debug = true;
        }
    }

    std::vector<Car> cars(3);
    for (int i = 0; i < cars.size(); i++) {
        initCar(cars[i], i + 1, debug);
    }

    assignCars(cars);

    // 保存信息到文件
    std::ofstream out("car_config.txt");
    if (out.is_open()) {
        for (const auto& car : cars) {
            car.save(out);
            out << "\n";
        }
        out.close();
        std::cout << "配置已保存到 car_config.txt" << std::endl;
    } else {
        std::cerr << "无法打开文件进行写入" << std::endl;
    }

    return 0;
}